#!/bin/sh
# -*- tcl -*- \
exec tclsh8.3 "$0" "$@"

# makesite.tcl --
#
#	A small utility for creating the XSLT Standard Library website.
#	We use Tcl because it is cross-platform and simple, unlike Make.
#
# Requirements:
#	Zwik installed in $home/../Zwik
#	DocBook XSL stylesheets in $home/../docbook/dbx

# Find the toplevel directory for XSLTSL

set home [file dirname [file dirname [file join [pwd] [info script]]]]

# We need the tools package to know how to run XSLT processors
# (This is actually part of Zwik and is not included)

lappend auto_path [file join [file dirname $home] Zwik lib]
package require tools
::tools::init [file join [file dirname $home] Zwik]

# Extract the DocBook from each module

foreach module [glob [file join $home *.xsl]] {
    set modulename [file root [file tail $module]]
    puts "Processing module $modulename"
    if {![file exists $modulename.html] || \
	    [file mtime $module] > [file mtime $modulename.html] || \
	    [file mtime $module] > [file mtime makesite.tcl] || \
	    [file mtime $module] > [file mtime xsl2jref.xsl] || \
	    [file mtime $module] > [file mtime jrefhtml.xsl]} {
	::tools::xslt $module xsl2jref.xsl dummy.xml output-file $modulename.xml
	::tools::xslt $modulename.xml jrefhtml.xsl $modulename.html
    }
}

# Convert other documentation

foreach doc [glob [file join $home *.xml]] {
    set docname [file root [file tail $doc]]
    puts "Processing document $docname"
    if {![file exists $docname.html] || \
	    [file mtime $doc] > [file mtime makesite.tcl] || \
	    [file mtime $doc] > [file mtime $docname.html] || \
	    [file mtime $doc] > [file mtime jrefhtml.xsl]} {
	::tools::xslt $doc jrefhtml.xsl $docname.html
    }
}

# Process test doco as well

puts "Processing test documentation"
if {![file exists [file join test test.html]] || \
	[file mtime [file join .. test test.xsl]] > [file mtime [file join test test.html]] || \
	[file mtime [file join .. test test.xsl]] > [file mtime makesite.tcl] || \
        [file mtime [file join .. test test.xsl]] > [file mtime xsl2jref.xsl] || \
        [file mtime [file join .. test test.xsl]] > [file mtime jrefhtml.xsl]} {
    ::tools::xslt ../test/test.xsl xsl2jref.xsl dummy.xml output-file test/test.xml includestylesheets 0
    ::tools::xslt test/test.xml jrefhtml.xsl test/test.html
}

catch {file delete dummy.xml}
